//------------------------------------------------------------------------------
//	file
//		dsp_1z.c
//
//	brief
//		One-Zero filter implementation file
//
//	description
//		This is a one-zero filter file.
//		It operates on stereo files.
//
//	$Id: dsp_1z.c 312 2014-12-06 04:06:02Z ac.verbeck@gmail.com $
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//	The MIT License (MIT)
//
//	Copyright (c) 2014 A.C. Verbeck
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.

//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>

#include <stdint.h>
#include <stdbool.h>

#include <sndfile.h>

#include "dsp_1z.h"

//------------------------------------------------------------------------------
// Local data
//------------------------------------------------------------------------------
static SF_INFO	o_sf;
static PARAM_T	p_save;
static double	atten_dB = 0.0;

//------------------------------------------------------------------------------
//	brief
//		parameter initialization, processing, and end functions
//
//	description
//		These functions are called as follows:
//		dsp_param_init:		is called at the start of DSP processing
//		dsp_param_process:	is called after each audio buffer is processed
//		dsp_param_end:		is called at the end of DSP processing
//
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//	brief
//		DSP parameter initialization
//
//	description
//		This function stores the PARAM_T structure locally.
//		It then processes any local variables to prepare for
//		DSP processing.
//
//	parameters
//		PARAM_T*	Pointer to the main program parameter block
//
//	return value
//		none
//------------------------------------------------------------------------------
void dsp_param_init(PARAM_T* p_in)
{
	p_save = *p_in;																//	Save the parameter block

	printf("param display:\n");													//	Print parameter information
	printf("   Attenuation: %f\n", p_save.amp);
	printf("   Sample Rate: %d\n", p_save.sr);
	printf("            b0: %f\n", p_save.b0);
	printf("            b1: %f\n", p_save.b1);

	atten_dB = pow(10.0, p_save.amp/20.0);										//	Calculate attenuation
}

//------------------------------------------------------------------------------
//	brief
//		DSP parameter processing
//
//	description
//		This function is called after each buffer is processed.
//		It is used to update any local parameters.
//		For a 44.1kHz sample rate, this function is called every
//		23.22ms
//
//	parameters
//		none
//
//	return value
//		none
//------------------------------------------------------------------------------
void dsp_param_process(void)
{
}

//------------------------------------------------------------------------------
//	brief
//		DSP parameter end processing
//
//	description
//		This function is called at the end of the application.
//		It is used to update any local parameters before the  
//		effect tail is processed.
//
//	parameters
//		none
//
//	return value
//		none
//------------------------------------------------------------------------------
void dsp_param_end(void)
{
}

//------------------------------------------------------------------------------
//	brief
//		buffer initialization, processing, and end functions
//
//	description
//		These functions are called as follows:
//		dsp_buff_init:		is called at the start of DSP processing
//		dsp_buff_process:	is called to process each buffer
//		dsp_buff_end:		is called at the end of DSP processing
//
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//	brief
//		dsp buffer init function
//
//	param
//		SF_INFO* o_sfinfo: Pointer to output sound-file information structure
//
//	return
//		none
//------------------------------------------------------------------------------
void dsp_buff_init(SF_INFO* o_sfinfo)
{
	o_sf = *o_sfinfo;															//	Save output sound file info

	if (o_sf.channels != 2) {													//	How many channels does this effect support
		printf("This DSP effect can only generate stereo files\n");
		exit(0);
	}
	if ((o_sf.format & SF_FORMAT_SUBMASK) != SF_FORMAT_FLOAT) {					//	Data format
		printf("This DSP effect can only generate 32-bit floating point files\n");
		exit(0);
	}
	if ((o_sf.format & SF_FORMAT_ENDMASK) != SF_ENDIAN_FILE) {					//	Endian file types
		printf("This DSP effect can only generate native endian files\n");
		exit(0);
	}
}

//------------------------------------------------------------------------------
//	brief
//		dsp buffer processing function
//
//	param
//		double*		b_out:	Buffer to place the modified data
//		double*		b_in:	Incoming audio data buffer
//		sf_count_t	ct:		Number of items to process in this function
//
//	return
//		none
//------------------------------------------------------------------------------
static double l_delay=0.0;
static double r_delay=0.0;

void dsp_buff_process(double* b_out, double* b_in, sf_count_t ct)
{
	double l_in, r_in;
	double l_out, r_out;

	assert((ct % 2) == 0);														//	Verify buffer contains an even sample count
	ct /= 2;																	//	Stereo: each iteration uses two samples

	for (sf_count_t i=0; i<ct; i++)	{											//	Loop through the input buffer (in stereo)
		l_in = *b_in++;															//	Read the left sample
		r_in = *b_in++;															//	Read the right sample

//------------------------------------------------------------------------------
//	input attenuation
//------------------------------------------------------------------------------
		l_in *= atten_dB;														//	Attenuate the left
		r_in *= atten_dB;														//	Attenuate the right

//------------------------------------------------------------------------------
//	filter
//------------------------------------------------------------------------------
 		l_out = l_delay*p_save.b1 + l_in*p_save.b0;								//	calculate the filter
		l_delay = l_in;															//	save the input value in the delay

		r_out = r_delay*p_save.b1 + r_in*p_save.b0;								//	calculate the filter
		r_delay = r_in;															//	save the input value in the delay

//------------------------------------------------------------------------------
//	output
//------------------------------------------------------------------------------
		*b_out++ = l_out;														//	Write the left output value
		*b_out++ = r_out;														//	Write the right output value
	}
}

//------------------------------------------------------------------------------
//	brief
//		dsp buffer end processing function
//
//	description
//		This function is called at the end of the application.
//		It is used to generate the DSP effect tail.  
//
//	param
//		none
//
//	return
//		none
//------------------------------------------------------------------------------
void dsp_buff_end(void)
{
}

//
//	End: dsp_1z.c
//
